<?php
/* 
 * 首页推荐 网站新闻 操作类
 * 程序编写：pennyowen/pepsi.zh
 * 联系邮箱：penyowen#gmail.com/pepsi.zh#gmail.com
 * 修改时间：2008-9-30
 */
if (!defined('IN_SITE')){ die('Hacking attempt!'); }
interface NewsDbInter{
	### 功能：提交一个新闻记录  参数：$post 提交的内容 #####
    function submitRecord($post);
	### 功能：通过一个记录id获得一条新闻记录  参数：$id 记录id #####
    function getOneRecord($id);
	### 功能：获得某些条件的新闻  参数：$where 条件 $order；排序参照 $limit：返回的记录条数 #####
	function getRecordItem($where="",$order='Articleid desc',$deleted='n',$limit=10);
    ### 功能：获得某个类型的新闻  参数：$typeid 新闻类型id；$limit 返回的记录条数 #####
    function getRecordOfOneType($typeid='',$tlong=0,$limit=10,$ttype=true,$order='Articleid desc');
    ### 功能：删除新闻记录  参数：$id 新闻id，可以是数组也可以是单id #####
    function deleteRecord($id);
    ####--------------------新闻类型管理------------------------------####
    #### 注意 ： 大类改小类的适合一定要小心，很可能会出错 #####
    ### 功能：提交一个新闻类型记录  参数：$post 提交的内容 #####
    function submitTypeRecord($post);
    ### 功能：删除新闻类型记录  参数：$id 新闻类型id #####
    function deleteTypeRecord($id);
    ### 功能：得到新闻类别列表  参数：$type 新闻类别的类型 0首页推荐类型 1为新闻类型 #####
    function getTypeList($type=0);
    ### 功能：通过一个记录id获得一条新闻类型记录  参数：$id 记录id #####
    function getOneType($id);
}
class news implements NewsDbInter
{
	public  $error = array();
	public  $action = '提交';
	function submitRecord($post){
		global $dbconn,$dbtable;
		$post = acrossTrim($post);
		$flag = true;
		if ( empty($post[Atitle])){
			$flag = false;
			$this->error[e_Atitle] = '标题不能为空';
		}elseif (strlen($post[Atitle]) > 180 ){
			$flag = false;
			$this->error[e_Atitle] = '标题字节小于180，60个汉字';
		}
		if (empty($post[Acontent])){
			$flag = false;
			$this->error[e_Acontent] = '新闻内容不能为空';
		}
		if (empty($post[Cid])){
			$flag = false;
			$this->error[e_Cid] = '新闻类别不能为空';
		}
		
		if ($post[unsetValue]){
			foreach ($post[unsetValue] as $value){
				$post[$value] = '';
			}
		}
		unset($post[unsetValue]);
		
		if ($flag){
			if(is_uploaded_file($_FILES['imagefile']['tmp_name'])){
				srand(time(0)); 
				$random =rand(1,100); 
				$upload_file_name = date("YmdHis",time()).$random;
				$filetype = getPicType($_FILES['imagefile']['type']);
				$save_path = UPLOAD_ROOT.$upload_file_name.$filetype;
				if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$save_path)){
					$post[Aphoto] = $upload_file_name.$filetype;
				}
			}
			if(is_uploaded_file($_FILES['appendixfile']['tmp_name'])){
				srand(time(0)); 
				$random =rand(1,100); 
				$upload_file_name = date("YmdHis",time()).$random;
				$filetype = '.'.end(explode('.',$_FILES['appendixfile']['name']));
				$save_path = APPENDIX_ROOT.$upload_file_name.$filetype;
				if(move_uploaded_file($_FILES['appendixfile']['tmp_name'],$save_path)){
					$post[Aappendix] = $upload_file_name.$filetype;
				}
			}
			$post[Astatic] = 'n';
			if ($post[Articleid]){
				$this->action = '修改';
				return $dbconn->Query_update($post,$dbtable->news,array('Articleid'=>$post[Articleid]));
			}else {
				$post[Adate] = date("Y-m-d H:i:s");
				$this->action = '添加';
				return $dbconn->Query_adate($post,$dbtable->news);
			}
		}
		return false;
	}
	function getOneRecord($id,$deleted='n'){
		global $dbconn,$dbtable;
		if ($id){ $id = intval($id); }else{ return false ;}
		if ($deleted=='n'){
			$deleted = "and Adeleted='n'";
		}elseif ($deleted=='y'){
			$deleted = "and Adeleted='y'";
		}else {
			$deleted = '';
		}
		$query = "select * from ".$dbtable->news." where Articleid=$id $deleted limit 1";
		$record = $dbconn->Getonerecord($query);
		if ($record[Aphoto] && file_exists(UPLOAD_ROOT.$record[Aphoto])){
			$record[Aphoto_show] = UPLOAD_URL.$record[Aphoto];
		}else {
			$record[Aphoto] = '';
		}
		if ($record[Aappendix] && file_exists(APPENDIX_ROOT.$record[Aappendix])){
			$record[Aappendix_show] = APPENDIX_URL.$record[Aappendix];
		}else {
			$record[Aappendix] = '';
		}
		return $record;
	}

	function getRecordItem($where="",$order='Articleid desc',$deleted='n',$limit=10,$tlong=0,$ttype=true){
		global $dbconn,$dbtable;
		if ($where=="")return false;
		if ($deleted=='n'){
			$deleted = "and Adeleted='n'";
		}elseif ($deleted=='y'){
			$deleted = "and Adeleted='y'";
		}else {
			$deleted = '';
		}
		if ($limit){
			$limit = "limit 0,".intval($limit);
		}else {
			$limit = "";
		}
		if ($order){
			$order = "order by $order";
		}else {
			$order = '';
		}
		$query = "select Articleid,Atitle,Aphoto,Avideo,Adate,Ahit,Amonthhit from ".$dbtable->news." where $where $deleted $order $limit";
		$nowQueryId = $dbconn->Query($query);
		while(($row = $dbconn->next_record($nowQueryId))){
			if ($row[Aphoto] && file_exists(UPLOAD_ROOT.$row[Aphoto])){
				$row[Aphoto_show] = UPLOAD_URL.$row[Aphoto];
			}else {
				$row[Aphoto] = '';
			}
			if ($tlong){
				$row[Atitle_cut] = cut_str($row[Atitle],$tlong,0,"utf-8",$ttype);
			}else {
				$row[Atitle_cut] = $row[Atitle];
			}
			$record[] = $row;
		}
		return $record;
	}

	function getRecordOfOneType($cid='',$tlong=0,$limit=10,$ttype=true,$order='Articleid desc'){
		global $dbconn,$dbtable;
		if ($cid){
			if (($cidarray = $this->getTypeList($cid))){
				$cidarray = array_flip($cidarray);
				$cidarray[] = $cid;	///这里加上他自己本身
				$cid = "and Cid in(".implode(',',$cidarray).")";
			}else {
				$cid = "and Cid=$cid";
			}
		}else {
			$cid = '';
		}
		if ($limit){
			$limit = "limit 0,".intval($limit);
		}else {
			$limit = "";
		}
		if ($order){
			$order = "order by $order";
		}else {
			$order = '';
		}
		$query = "select Articleid,Atitle,Aphoto,Aappendix,Avideo,Adate,Ahit,Amonthhit from ".$dbtable->news." where Adeleted='n' $cid $order $limit";
		$nowQueryId = $dbconn->Query($query);
		while(($row = $dbconn->next_record($nowQueryId))){
			$row[Adate] = date("Y-m-d",strtotime($row[Adate]));
			$row[Atitle] = specialhtml($row[Atitle]);
			if ($tlong){
				$row[Atitle_cut] = cut_str($row[Atitle],$tlong,0,"utf-8",$ttype);
			}else {
				$row[Atitle_cut] = $row[Atitle];
			}
			$record[] = $row;
		}
		if (empty($record)){$record[0][Atitle_cut] = "此频道新闻信息整理添加中……";}
		return $record;
	}
	function deleteRecord($id,$type=true){
		global $dbconn,$dbtable;
		if (empty($id)){ return false ;}
		if (is_array($id)){
			$where  = "Articleid in (".implode(',',$id).")";
		}else {
			$where  = "Articleid=$id limit 1";
		}
		if ($type){
			$delete = 'y';
		}else {
			$delete = 'n';
		}
		return $dbconn->Query_update(array('Adeleted'=>$delete),$dbtable->news,"where ".$where);
	}
    ####--------------------新闻类型管理------------------------------####
    function submitTypeRecord($post){
    	global $dbconn,$dbtable;
    	$post = acrossTrim($post);
    	$flag = true;
    	if (strlen($post[Ccataname]) < 1){
    		$flag = false;
			$this->error[e_Ccataname] = '新闻类别名称不能为空';
    	}
    	if ($flag){
    		if ($post[Cid]){
    			$this->action = '修改';
    			$query = "select Cid from ".$dbtable->newscata." where Ccataname='".$post[Ccataname]."' and Cid!=".$post[CAid];
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_Ccataname] = '新闻类别名称已经存在了，请换一个';
    			}else {
    				///////为#remarks 001做准备
    				$query = "select Cbelong from ".$dbtable->newscata." where Cid=".$post[Cid]." limit 1";
    				$Cbelong = $dbconn->Getonefield($query);
    				$sunType = $this->getTypeList($post[Cid],0);
    				////////如果是父类是自己本身就注销父类值
					if ($post[Cbelong] == $post[Cid]){
    					unset($post[Cbelong]);
    				}
					if ($dbconn->Query_update($post,$dbtable->newscata,array('Cid'=>$post[Cid]))){
						///#remarks 001///大类改小类，并且还是在同一类别所属下面的的时候。要做下面的处理
    				 	if (array_key_exists($post[Cbelong],$sunType)){
    				 		$query = "update ".$dbtable->newscata." set Cbelong=$Cbelong where Cbelong=".$post[Cid];
    						$dbconn->Query($query);
    				 	}
    				 	return true;
    				 }else {
    				 	return false;
    				 }
    			}
    		}else {
    			$this->action = '添加';
    			$query = "select Cid from ".$dbtable->newscata." where Ccataname='".$post[Ccataname]."'";
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_Ccataname] = '新闻类别名称已经存在了，请换一个';
    			}else {
    				return $dbconn->Query_adate($post,$dbtable->newscata);
    			}
    		}
    	}
    	return false;
    }
    function deleteTypeRecord($id){
    	global $dbconn,$dbtable;
    	if (empty($id)) { return false ;}
    	else { $id = intval($id); }
    	$query = "select Cbelong from ".$dbtable->newscata." where Cid=$id";
    	$Cbelong = $dbconn->Getonefield($query);
    	$query = "update ".$dbtable->newscata." set Cbelong=$Cbelong where Cbelong=$id";
    	$dbconn->Query($query);
    	return deleteRecord($dbtable->newscata,"Cid=$id");
    }
    function getTypeList($Cbelong=0,$returnType=1,$num=0){
    	global $dbconn,$dbtable;
    	$array = array();
    	$query = "select Cid,Ccataname from ".$dbtable->newscata." where Cbelong='$Cbelong' order by Cid asc";
    	//exit($query);
    	$nowQueryid = $dbconn->Query($query);
    	if ($returnType){
	    	for ($num_i=0;$num_i<$num;$num_i++){
	    		$cataname .= '&nbsp;&nbsp;';
	    	}
	    	if ($cataname)
	    	$cataname .= '|--';
    	}
    	++$num;
    	while (($row = $dbconn->next_record($nowQueryid))){
    		$array[$row[Cid]] = $cataname.$row[Ccataname];
    		if (($min_array = $this->getTypeList($row[Cid],$returnType,$num))){
    			$array = $array+$min_array;	/////这里不能用array_merge 因为$row[Cid]会被当做序号键值来处理
    		}
    	}
    	return $array;
    }
    function getOneType($id){
    	global $dbconn,$dbtable;
    	if (empty($id)){ return false ;}
    	$query = "select * from ".$dbtable->newscata." where Cid=$id limit 1";
    	return $dbconn->Getonerecord($query);
    }
}
?>